package com.zz.hhbl.utils

import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.widget.EditText

/**
 * Copyright © 2025 zhun All rights reserved.
 * Created by ZhaoZhun on 2025/3/19 14:18.
 * @author: ZhaoZhun 1820022519@qq.com
 * @version: V1.0
 */
class NumberInputTextWatcher(val editText: EditText):TextWatcher {
    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

    }

    override fun afterTextChanged(text: Editable?) {
        text?.let {
            val newInput = "${it.toString().toIntOrNull()?:""}"
            editText.removeTextChangedListener(this)
            editText.setText(newInput)
            editText.setSelection(newInput.length)
            editText.addTextChangedListener(this)
        }
    }
}